package org.cmg.study.spring.controller;

import org.cmg.study.spring.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

/**
 * @author CMG
 * @version 1.0
 * @cLassName UserController
 * @description
 * @date 2021/9/6 11:24
 **/
@Controller
public class UserController {

    @RequestMapping("/validate")
    public String validate(@Valid User user, BindingResult result){
        //如果有异常信息
        if(result.hasErrors()){
            //获取异常对象
            List<ObjectError> errors = result.getAllErrors();
            //将异常信息输出
            for(ObjectError error : errors){
                System.out.println(error.getDefaultMessage());
            }
        }
        return "index";
    }

    @RequestMapping("/addUser")
    public String add(){
        return "addUser";
    }
}
